import {ajax,getUserInfo} from '@/common/common.js'
import {bpmUrl} from "@/common/config.js"
//初始赋值
function  formonload(list,formData,bussiness){
	let currentNodeid = bussiness.currentNodeid
	let arr = list.map(item => {
		if(['ProjectID'].indexOf(item.name) != -1 ){
				item.disabled = true
		}
		return item
	})
	let IsFirstNodeFlag = bussiness.IsFirstNodeFlag
	if( IsFirstNodeFlag != 'true' ){
		initform(list,formData,bussiness)
	}else{
	    changeBackType(list,formData,bussiness)
	}
	return arr
}
function initform(list,formData,bussiness){
    //是否有实物退回
    var Back_Type = "[X]Back_Type[/X]"
	//客户变更税号、开票抬头
	var reason_id = "[X]Back_Reason[/X]"
	list.map(item => {
		if(Back_Type=="无实物退回"){
			if(['receive_','fileInvoice'].indexOf(item.name) != -1){
				item.hide = '1' //隐藏
			}
		}else{
			if(['receive_','fileInvoice'].indexOf(item.name) != -1){
				item.hide = '0' //显示
			}
		}
		return item
	})
}

function changeBackType(list,formData,bussiness){
    var Back_TYPE = formData.Back_TYPE
    var dept;
	let user = getUserInfo()
	let params = {
		paramasMap: {
			OldDepart: formData.OldDepart
		},
		headersMap: {
			sysid: "bpm",
			syspwd: "pass",
			userid: user.bpmUid
		},
		url: bpmUrl + '/rest/appform/CRMVR/R_CRMVR_B283'
	}
    //获取用户所在的部门
	ajax({
		url:'/bpm/get',
		method: 'POST',
		data: params,
		success:res => {
			dept = res.data.data.trim()
		}
	})
    var currentNodeid = bussiness.currentNodeid
	
	list.map(item => {
		if(currentNodeid=="T10002"){
			if(Back_TYPE=="有实物退回"){
				//显示取件相关字段
				//显示客户存储温度记录filerecord
				//隐藏是否先行赔付
				//隐藏客户变更税号、开票抬头附件上传
				if(['IS_Payed','fileInvoice'].indexOf(item.name) != -1){
						item.hide = '1' //隐藏
				}
				if(['receive_','filerecord'].indexOf(item.name) != -1){
					item.hide = '0' //显示
				}
				
				formData.ProjectID = "试剂退货申请单"
				if(['ProjectID'].indexOf(item.name) != -1){
					item.disabled = true
				}
				//加载退货原因
				if(dept == "001021001"||dept == "001021002"||dept == "001021003"){//物流加客服
					let arr = [
					  {id:'发货错误-仅限物流部提交',text:'【发货错误-仅限物流部提交】'},
					  {id:'制单错误-仅限客服部提交',text:'【制单错误-仅限客服部提交】'},
                    ]
					arr = arr.map(item => {
						item.label = item.text
						return item
					})
					list.map(item => {
						if(item.name == 'Back_Reason'){
							item.optiona.data = arr
						}
					})
				}else if(dept == "001008004"||dept == "001002006"||dept == "001002001"||dept == "001002002"||dept == "001002003"){
					  let arr = [{id:'客户提交订单错误',text:'【客户提交订单错误】'},
                          {id:'退换货-商业类',text:'【退换货-商业类】'},
                          {id:'退换货-投诉类',text:'【退换货-投诉类】'},
                        ]
					  arr = arr.map(item => {
						item.label = item.text
						return item
					  })
					  list.map(item => {
						if(item.name == 'Back_Reason'){
							item.optiona.data = arr
						}
					  })
				}else{
					
				}
				
			}else if(Back_TYPE=="无实物退回"){
				//隐藏取件相关字段
				//隐藏先行赔付
				//隐藏客户存储温度记录filerecord
				if(['receive_','IS_Payed','filerecord'].indexOf(item.name) != -1){
						item.hide = '1' //隐藏
				}
				formData.ProjectID = "试剂发货调整单"
				if(['ProjectID'].indexOf(item.name) != -1){
					item.disabled = true
				}
				
				//加载退货原因
				if(dept == "001008004"||dept == "001002006"||dept == "001002001"||dept == "001002002"||dept == "001002003"){
				  let arr = [
					  {id:'一张发票拆分多张开具',text:'【一张发票拆分多张开具】'},
					  {id:'票据中的产品价格变更',text:'【票据中的产品价格变更】'},
					  {id:'销售转赠送',text:'【销售转赠送】'},
					  {id:'客服制单错误-仅限客服提交',text:'【客服制单错误-仅限客服提交！】'},
					  {id:'开票类型变更',text:'【开票类型变更！】'},
					  {id:'客户转换A-B',text:'【客户转换A-B！】'},
					  {id:'发票重开-货品规格型号变更',text:'【发票重开-货品规格型号变更】'},
					  {id:'发票重开-税号变更',text:'【发票重开-税号变更】'},
					  {id:'发票重开-开票日期变更',text:'【发票重开-开票日期变更】'},
					  {id:'发票重开-客户更名开票抬头调整',text:'【发票重开-客户更名/开票抬头调整(A-A*)】'},
					  {id:'发票重开-人为开错发票',text:'【发票重开-人为开错发票】'},
                    ]
				  arr = arr.map(item => {
				  	item.label = item.text
				  	return item
				  })
				  list.map(item => {
				  	if(item.name == 'Back_Reason'){
				  		item.optiona.data = arr
				  	}
				  })
				}else{
					//加载退货原因
					list.map(item => {
						if(item.name == 'Back_Reason'){
							item.optiona.data = []
						}
					})
				}
			}else{
				//隐藏取件相关字段
				//隐藏客户变更税号、开票抬头附件上传
				//隐藏先行赔付
				//隐藏客户存储温度记录filerecord
				if(['receive_','fileInvoice','IS_Payed','filerecord'].indexOf(item.name) != -1){
						item.hide = '1' //隐藏
				}
				//加载退货原因
				list.map(item => {
					if(item.name == 'Back_Reason'){
						item.optiona.data = []
					}
				})
			} 
		}
	})
}
function backReasonOnSelect(item,formData,list){
	if(['IS_Payed','fileInvoice'].indexOf(item.name) != -1){
			item.hide = '1' //隐藏
	}
	var reason_id = formData.Back_Reason;
	
	if(reason_id=="退换货-商业类"||reason_id=="退换货-投诉类"){
		if(['IS_Payed'].indexOf(item.name) != -1){
			item.hide = '0' //显示
		}
	}
    if(reason_id=='发票重开-税号变更'||reason_id=='发票重开-客户更名开票抬头调整'){
		if(['fileInvoice'].indexOf(item.name) != -1){
			item.hide = '0' //显示
		}
	}
}

function getHelp(){
    var url="form?wf_num=F_CRMVR_A031";
    var swidth=screen.availWidth;
    var sheight=screen.availHeight;
    var wwidth=1000;
    var wheight=800;	
    var wleft=(swidth/2-0)-wwidth/2;
    var wtop=(sheight/2-0)-wheight/2;
    window.open(url,'pwin','Width='+wwidth+'px,Height='+wheight+'px,Left='+wleft+',Top='+wtop+',status=no,resizable=yes,resezie=no');
}


//给相关字段赋值
function fz(obj,item,formData,list){
	formData.cCusName = obj.cCusName
	formData.cCusCode = obj.cCusCode
	formData.comCode1 = obj.comCode1
	let user = getUserInfo()
	let params = {
		paramasMap: {
			cCusCode: obj.cCusCode,
			comCode: ""
		},
		headersMap: {
			sysid: "bpm",
			syspwd: "pass",
			userid: user.bpmUid
		},
		url: bpmUrl + '/rest/appform/getDepartmentByCondition'
	}
	// // 省份
	ajax({
		url:'/bpm/get',
		method: 'POST',
		data: params,
		success:res => {
			formData.Department = res.data.data.Department
		}
	})
	// 客户类别
	params.url = bpmUrl + '/rest/appform/getcCusDefine6'
	ajax({
		url:'/bpm/get',
		method: 'POST',
		data: params,
		success:res => {
			formData.cCusDefine6 = res.data.data.cCusDefine6
		}
	})
	//传真号码(cCusFax)
	params.url = bpmUrl + '/rest/appform/getcCusFax'
	ajax({
		url:'/bpm/get',
		method: 'POST',
		data: params,
		success:res => {
			formData.cCusFax = res.data.data.cCusFax
		}
	})
	//电话(cCusPhone)
	params.url = bpmUrl + '/rest/appform/getcCusPhone'
	ajax({
		url:'/bpm/get',
		method: 'POST',
		data: params,
		success:res => {
			formData.cCusPhone = res.data.data.cCusPhone
		}
	})
	//手机(cCusHand)
	params.url = bpmUrl + '/rest/appform/getcCusHand'
	ajax({
		url:'/bpm/get',
		method: 'POST',
		data: params,
		success:res => {
			formData.cCusHand = res.data.data.cCusHand
		}
	})
	//客户类型(Cust_TYPE)
	params.url = bpmUrl + '/rest/appform/getcCusHand'
	ajax({
		url:'/bpm/get',
		method: 'POST',
		data: params,
		success:res => {
			formData.cCusHand = res.data.data.cCusHand
		}
	})
	// formData.Cust_TYPE = '客户类型'
	//开票类型(cCusDefine2)
	params.url = bpmUrl + '/rest/appform/getcCusDefine2'
	ajax({
		url:'/bpm/get',
		method: 'POST',
		data: params,
		success:res => {
			formData.cCusDefine2 = res.data.data.cCusDefine2
		}
	})
	//合作情况(ccdefine7)
	// params.url = bpmUrl + '/rest/appform/getccdefine7'
	// ajax({
	// 	url:'/bpm/get',
	// 	method: 'POST',
	// 	data: params,
	// 	success:res => {
	// 		formData.ccdefine7 = res.data.data.ccdefine7
	// 	}
	// })
	
	//根据被授权医院名称返回其所对应的大区
	params.url = bpmUrl + '/rest/appform/CRMVR/R_CRMVR_B105'
	ajax({
		url:'/bpm/get',
		method: 'POST',
		data: {
			paramasMap:{
				cCusCode:formData.cCusCode,
				OldDepart:formData.OldDepart
			}
		},
		success:res => {
			formData.OldDepart = res.data.data.OldDepart
		}
	})
	
	list.map(item => {
		if(['comCode1',
			'cCusCode',
			'Department',
			'cCusFax',
			'cCusPhone',
			'cCusHand',
			'cCusDefine2',
			'cCusDefine6',
			'cCusDefine5',
			'OldDepart'
			].indexOf(item.name) != -1){
			item.disabled = true // 设置字段disabled
		}
		return item
	})
}

function formonsubmit(list,formData,bussiness){
	let flag = true
	let currentNodeid = bussiness.currentNodeid
	if(currentNodeid == "T10002"){
		//验证客户编码是否存在
		var cCusCode = formData.cCusCode;
		if(cCusCode==""){
			uni.showToast({
				title: "【请填写客户编码！】",
				icon:'none'
			})
			return false;
		}
		//验证客户编码是否存在
		var cCusName = formData.cCusName;
		if(cCusName==0){
			uni.showToast({
				title:"【客户名称和编码有误，请检查后重新输入！】",
				icon:'none'
			})
			return false;
		}         
		//验证数量字段不能为空
		var docunid = bussiness.docUnid;
		let user = getUserInfo()
		let paramas = {
			paramasMap: {
				docunid:docunid
			},
			headersMap: {
				sysid: "bpm",
				syspwd: "pass",
				userid: user.bpmUid
			},
			url: bpmUrl + '/rest/appform/CRMVR/R_CRMVR_B120'
		}
		ajax({
			url:'/bpm/get',
			method: 'POST',
			data: paramas,
			success:res => {
				if(res.data.data= 1){
				//alert("【请填写表体中数量！】");
				//return false;
				}
			}
		})
		//验证子表信息不为空
		paramas.url = bpmUrl + '/rest/appform/CRMVR/R_CRMVR_B102'
		ajax({
			url:'/bpm/get',
			method: 'POST',
			data: paramas,
			success:res => {
				if(res.data.data= 1){
				//alert("【请填写表体中数量！】");
				//return false;
				}
			}
		})
		
		//验证预算名称不能为空
		let BudgetCode = formData.BudgetCode
		if(BudgetCode==""){
			uni.showToast({
				title:"请选择预算名称",
				icon:'none'
			})
			return false;
		}
		
		//判断授权客户+产品列表不能为空
		// if(){
		// 	uni.showToast({
		// 		title: "【请填写表体信息！】",
					// icon:'none'
		// 	})
		// 	return false;
		// }
		//验证产品和授权有效期不能为空
		// alert("【请填写【授权客户+产品列表】中的产品信息！】");
		// alert("【请填写【授权客户+产品列表】中的授权有效期！】");
		
	}
	list = list.map(item => {
		if([
			'cCusCode',
			'comCode1',
			'Department',
			'OldDepart',
			'ProjectID',
			].indexOf(item.name) != -1){
			item.disabled = false
		}
		return item
	})
	return flag;
}



//模糊搜索客户列表
function getCustomerName(queryString,formData, cb) {
	console.log(queryString, cb)
	let user = getUserInfo()
	let paramas = {
		paramasMap: {
			cCusName: queryString
		},
		headersMap: {
			sysid: "bpm",
			syspwd: "pass",
			userid: user.bpmUid
		},
		url: bpmUrl + '/rest/appform/getCustomerInfo'
	}
	ajax({
		url: '/bpm/get',
		method: 'POST',
		data: paramas,
		success: res => {
			console.log(res)
			let data = res.data.data.map(item => {
				item.id = item.cCusCode
				item.name = item.cCusName
				return item
			})
			console.log(data)
			cb(data)
		}
	})
}


export default {
	formonload,
	fz,
	initform,
	changeBackType,
	formonsubmit,
	getCustomerName
}